import math

from sklearn.model_selection import train_test_split
from sklearn.multioutput import MultiOutputRegressor
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error,make_scorer
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_absolute_percentage_error, adjusted_rand_score

from src.python.util.getResoures import getData


def run():
    # 获取数据
    x_train, x_test, y_train, y_test = getData()
    print("------------------------------------------- Ridge岭回归预测模型：-------------------------------------------")
    # 准备搜索最佳参数
    aplha = np.arange(0, 0.00001, 0.0000001)
    # 搜索最佳参数
    minMae = 99999
    minAplha = -1
    for i in range(len(aplha)):
        rd = Ridge(aplha[i], normalize=True)
        model = MultiOutputRegressor(rd)
        model.fit(x_train, y_train)
        y_pre = model.predict(x_test)
        # 评估分数
        mae = mean_absolute_error(y_pred=y_pre, y_true=y_test)
        if mae < minMae:
            minMae = mae
            minAplha = aplha[i]
    # 最优Ridge岭回归预测模型模型
    rd = Ridge(minAplha, normalize=True)
    model = MultiOutputRegressor(rd)
    # 训练
    model.fit(x_train, y_train)
    y_pre = model.predict(x_test)
    # 评估分数
    mae = mean_absolute_error(y_pred=y_pre, y_true=y_test)
    print("平均绝对误差MAE:", mae)
    mse = mean_squared_error(y_pred=y_pre, y_true=y_test)
    print("均方根误差RMSE:", math.sqrt(mse))
    print("均方误差MSE:", mse)
    mape = mean_absolute_percentage_error(y_pred=y_pre, y_true=y_test)
    print("平均绝对百分比误差MAPE:", mape)


if __name__ == "__main__":
    run()